package v1

import (
	"github.com/gin-gonic/gin"
	"maya-service/bll"
	"maya-service/model"
	"maya-service/model/po"
	"maya-service/server/web/middleware"
	"maya-service/utils"
	"maya-service/utils/request"
)

var AiConsultStatics = &aiConsultStatics{}

func init() {
	RegisterRouter(AiConsultStatics)
}

type aiConsultStatics struct{}

// Init
func (a *aiConsultStatics) Init(r *gin.RouterGroup) {
	g := r.Group("/aiConsultStatics", middleware.Auth())
	{
		g.POST("/create", a.create)
		g.POST("/update", a.update)
		g.POST("/list", a.list)
		g.POST("/delete", a.delete)
		g.POST("/detail", a.find)
		g.POST("/statics", a.statics)
	}
}

// 统计
func (a *aiConsultStatics) statics(c *gin.Context) {
	var (
		in  = &po.AiConsultStaticsQueryReq{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	os := c.GetHeader("source")
	if os != "web" && in.UserId == "" {
		userId, err := request.GetUserId(c.Request.Context())
		if err != nil {
			c.Error(err)
			return
		}
		in.UserId = userId
	}

	params := &model.AiConsultStaticsListRequest{
		UserId: &in.UserId,
		Index:  1,
		Size:   10000,
	}

	if in.Level > 0 {
		params.Level = &in.Level
	}

	if in.Module > 0 {
		params.Module = &in.Module
	}

	// 统计列表
	list, err := bll.AiConsultStatics.List(c.Request.Context(), params)
	if err != nil {
		c.Error(err)
		return
	}

	out := &po.AiConsultStaticsQueryResp{
		UserId:     in.UserId,
		Level:      in.Level,
		Module:     in.Module,
		Count:      0,
		TotalScore: 0,
		MaxScore:   0,
		MinScore:   0,
	}

	maxScore := int64(0)
	minScore := int64(0)
	for i := 0; i < len(list.List); i++ {
		if i == 0 {
			maxScore = list.List[i].MaxScore
			minScore = list.List[i].MinScore
		}

		out.Count += list.List[i].Count
		out.TotalScore += list.List[i].TotalScore
		if list.List[i].MaxScore > maxScore {
			maxScore = list.List[i].MaxScore
		}

		if minScore == 0 {
			minScore = list.List[i].MinScore
		} else {
			if list.List[i].MinScore > 0 && list.List[i].MinScore < minScore {
				minScore = list.List[i].MinScore
			}

		}
	}

	out.MaxScore = maxScore
	out.MinScore = minScore

	gteScores := 0
	params1 := &model.AiConsultListRequest{
		UserId:    &in.UserId,
		GteScores: &gteScores,
		Index:     1,
		Size:      10000,
	}

	if in.Level > 0 {
		params1.Level = &in.Level
	}

	if in.Module > 0 {
		params1.Module = &in.Module
	}

	// 列表
	list1, _ := bll.AiConsult.List(c.Request.Context(), params1)
	if list1.Total > 0 {
		out.AvgScore = out.TotalScore / int64(list1.Total)
	}

	utils.ResponseOk(c, out)
	return

}

// create
func (a *aiConsultStatics) create(c *gin.Context) {
	var (
		in  = &model.AiConsultStaticsCreateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.AiConsultStatics.Create(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

// update
func (a *aiConsultStatics) update(c *gin.Context) {
	var (
		in  = &model.AiConsultStaticsUpdateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.AiConsultStatics.Update(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

// list
func (a *aiConsultStatics) list(c *gin.Context) {
	var (
		in  = &model.AiConsultStaticsListRequest{}
		out = &model.AiConsultStaticsListResponse{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if out, err = bll.AiConsultStatics.List(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, out)
}

// list
func (a *aiConsultStatics) find(c *gin.Context) {
	var (
		in  = &model.AiConsultStaticsInfoRequest{}
		out = &model.AiConsultStaticsInfo{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if in.UserId == "" {
		userId, err := request.GetUserId(c.Request.Context())
		if err != nil {
			c.Error(err)
			return
		}

		in.UserId = userId
	}

	if out, err = bll.AiConsultStatics.Find(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}

	gteScores := 0
	params := &model.AiConsultListRequest{
		UserId: &in.UserId,
		Scores: &gteScores,
		Index:  1,
		Size:   10000,
	}

	if in.Level > 0 {
		params.Level = &in.Level
	}

	if in.Module > 0 {
		params.Module = &in.Module
	}

	list, _ := bll.AiConsult.List(c.Request.Context(), params)
	if list.Total > 0 {
		out.AvgScore = out.TotalScore / int64(list.Total)
	}

	utils.ResponseOk(c, out)
}

// delete
func (a *aiConsultStatics) delete(c *gin.Context) {
	var (
		in  = &model.AiConsultStaticsDeleteRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.AiConsultStatics.Delete(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}
